package app

import (
	"base-gin/app/config"
	"base-gin/middleware"
	"base-gin/routers"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

var Srv *http.Server

func init() {
	gin.SetMode(config.App.Mode)
	//初始化gin服务
	app := gin.New()
	app.Use(middleware.GinLogger(), middleware.GinRecovery(true))
	//注册路由
	routers.RegistRoute(app)

	port := fmt.Sprintf(":%d", config.App.Port)
	Srv = new(http.Server)
	Srv.Addr = port
	Srv.Handler = app
}

func Run() {
	// 开启一个goroutine启动服务
	if err := Srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
		zap.L().Error("服务启动失败")
		panic(err)
	}
}
